/*
 * RiverFrame.java
 * Team qq 2011
 */
package com.google.code.timetrail.gui;

import javax.swing.JPanel;

import javax.swing.JLabel;

import java.awt.CardLayout;
import java.awt.Component;
import javax.swing.JRadioButton;

import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.backend.Event;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RiverFrame extends JPanel {
	
	/**
	 * The serialized version for the class
	 */
	private static final long serialVersionUID = -7962086205829620663L;

	private RiverFrameBackend riverFrameBackend;
	
	private JLabel titleText;
	
	private JLabel optionsText;
	
	private JRadioButton ferryRadioButton;
	
	private JRadioButton caulkRadioButton;
	
	private JRadioButton fordRadioButton;
	
	private final ButtonGroup optionsGroup = new ButtonGroup();
	
	private JButton confirmButton;
	
	private final Control gameControl;
	
	private JPanel myCD;
	
	public RiverFrame(Control gC, JPanel cd) {
		this.gameControl = gC;
		this.myCD = cd;
		riverFrameBackend = new RiverFrameBackend(gC);
		setLayout(null);
		
		titleText = new JLabel(riverFrameBackend.riverStatus());
		titleText.setAlignmentX(Component.CENTER_ALIGNMENT);
		titleText.setBounds(74, 42, 366, 34);
		add(titleText);
		
		optionsText = new JLabel(riverFrameBackend.optionsText());
		optionsText.setBounds(74, 79, 340, 34);
		add(optionsText);
		
		ferryRadioButton = new JRadioButton(riverFrameBackend.ferryText());
		//if(riverFrameBackend.canTakeFerry()){
				optionsGroup.add(ferryRadioButton);
				ferryRadioButton.setBounds(138, 109, 302, 44);
				add(ferryRadioButton);
		//}
		
		caulkRadioButton = new JRadioButton(riverFrameBackend.caulkText());
		caulkRadioButton.setSelected(true);
		optionsGroup.add(caulkRadioButton);
		caulkRadioButton.setBounds(138, 147, 302, 39);
		add(caulkRadioButton);
		
		fordRadioButton = new JRadioButton(riverFrameBackend.fordText());
		optionsGroup.add(fordRadioButton);
		fordRadioButton.setBounds(138, 187, 302, 39);
		add(fordRadioButton);
		
		confirmButton = new JButton(riverFrameBackend.confirmText());
		confirmButton.setEnabled(true);
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent action) {
				if(ferryRadioButton.isSelected()) {
					gameControl.setNextEvent(new Event(riverFrameBackend.getRiver().takeFerry(), gameControl));
				}
				else if(caulkRadioButton.isSelected()){
					gameControl.setNextEvent(new Event(riverFrameBackend.getRiver().caulk(), gameControl));
				}
				else if(fordRadioButton.isSelected()){
					gameControl.setNextEvent(new Event(riverFrameBackend.getRiver().ford(), gameControl));
				}
				else{
					gameControl.setNextEvent(new Event(riverFrameBackend.errorText(), gameControl));
				}
				
				
				EventFrameBackend eventFrameBackend = new EventFrameBackend(gameControl.getNextEvent());
				myCD.add(new EventFrame(gameControl, eventFrameBackend, myCD), "newEvent");
				gameControl.setNextEvent(new Event());
				CardLayout cd = (CardLayout) myCD.getLayout();
				cd.last(myCD);
			}
		});
		confirmButton.setBounds(138, 252, 89, 23);
		add(confirmButton);
	}
}
